package com.itboy.mall.pms.attrgroup.controller;

import java.util.List;
import java.util.Arrays;

import com.itboy.mall.pms.category.service.IMallCategoryService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.pms.attrgroup.vo.MallAttrGroupVo;
import com.itboy.mall.pms.attrgroup.bo.MallAttrGroupQueryBo;
import com.itboy.mall.pms.attrgroup.bo.MallAttrGroupAddBo;
import com.itboy.mall.pms.attrgroup.bo.MallAttrGroupEditBo;
import com.itboy.mall.pms.attrgroup.service.IMallAttrGroupService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 属性分组Controller
 *
 * @author itboy
 * @date 2021-07-17
 */
@Api(value = "属性分组控制器", tags = {"属性分组管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/attrGroup")
public class MallAttrGroupController extends BaseController {

    private final IMallAttrGroupService iMallAttrGroupService;

    private final IMallCategoryService iMallCategoryService;

    /**
     * 查询属性分组列表
     */
    @ApiOperation("查询属性分组列表")
    @PreAuthorize("@ss.hasPermi('mall:attrGroup:list')")
    @GetMapping("/list")
    public TableDataInfo<MallAttrGroupVo> list(@Validated MallAttrGroupQueryBo bo) {
        return iMallAttrGroupService.queryPageList(bo);
    }

    /**
     * 导出属性分组列表
     */
    @ApiOperation("导出属性分组列表")
    @PreAuthorize("@ss.hasPermi('mall:attrGroup:export')")
    @Log(title = "属性分组", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallAttrGroupVo> export(@Validated MallAttrGroupQueryBo bo) {
        List<MallAttrGroupVo> list = iMallAttrGroupService.queryList(bo);
        ExcelUtil<MallAttrGroupVo> util = new ExcelUtil<MallAttrGroupVo>(MallAttrGroupVo.class);
        return util.exportExcel(list, "属性分组");
    }

	/**
	 * 按照分类id获取分组列表
	 */
	@ApiOperation("按照分类id获取分组列表")
	@GetMapping("/getListByCatelogId/{catalogId}")
	public AjaxResult<List<MallAttrGroupVo>> getListByCatelogId(@NotNull(message = "分类id不能为空")
																	@PathVariable("catalogId") Long catalogId) {
		List<MallAttrGroupVo> mallAttrGroupVoList = iMallAttrGroupService.getListByCatelogId(catalogId);
		return AjaxResult.success(mallAttrGroupVoList);
	}

	/**
	 * 按照分类id获取分组和属性
	 */
	@ApiOperation("按照分类id获取分组和属性")
	@GetMapping("/getAttrGroupWithAttrByCatelogId/{catalogId}")
	public AjaxResult<List<MallAttrGroupVo>> getAttrGroupWithAttrByCatelogId(@NotNull(message = "分类id不能为空")
																@PathVariable("catalogId") Long catalogId) {
		List<MallAttrGroupVo> mallAttrGroupVoList = iMallAttrGroupService.getAttrGroupWithAttrByCatelogId(catalogId);
		return AjaxResult.success(mallAttrGroupVoList);
	}

    /**
     * 获取属性分组详细信息
     */
    @ApiOperation("获取属性分组详细信息")
    @PreAuthorize("@ss.hasPermi('mall:attrGroup:query')")
    @GetMapping("/{attrGroupId}")
    public AjaxResult<MallAttrGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("attrGroupId") Long attrGroupId) {
		MallAttrGroupVo mallAttrGroupVo = iMallAttrGroupService.queryById(attrGroupId);
		String[] recursionById = iMallCategoryService.getRecursionById(mallAttrGroupVo.getCatalogId());
		mallAttrGroupVo.setCatalogIds(recursionById);
		return AjaxResult.success(mallAttrGroupVo);
    }

    /**
     * 新增属性分组
     */
    @ApiOperation("新增属性分组")
    @PreAuthorize("@ss.hasPermi('mall:attrGroup:add')")
    @Log(title = "属性分组", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallAttrGroupAddBo bo) {
        return toAjax(iMallAttrGroupService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改属性分组
     */
    @ApiOperation("修改属性分组")
    @PreAuthorize("@ss.hasPermi('mall:attrGroup:edit')")
    @Log(title = "属性分组", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallAttrGroupEditBo bo) {
        return toAjax(iMallAttrGroupService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除属性分组
     */
    @ApiOperation("删除属性分组")
    @PreAuthorize("@ss.hasPermi('mall:attrGroup:remove')")
    @Log(title = "属性分组" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{attrGroupIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] attrGroupIds) {
        return toAjax(iMallAttrGroupService.deleteWithValidByIds(Arrays.asList(attrGroupIds), true) ? 1 : 0);
    }
}
